package com.diandian.backend.order.controller;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.order.param.OrderOfflineAddParam;
import com.diandian.backend.order.service.OrderOfflineService;
import com.diandian.base.boot.starter.dto.CheckSignResultDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.request.SignVerificationRequest;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.SignVerificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 线下订单记录表
 *
 * @author zyz diandian@qq.com
 * @since 1.0.0 2025-06-16
 */
@RestController
@RequestMapping("/order/offline")
public class OrderOfflineController {

    private static final Logger logger = LoggerFactory.getLogger(OrderOfflineController.class);

    @Autowired
    private OrderOfflineService orderOfflineService;

    /**
     * 接收零售系统推送的订单消息(无验签）
     * @return
     */
    @PostMapping("/sell/accept/inner")
    public BaseResult acceptSellOrderInfoInner(@RequestBody OrderOfflineAddParam param) {

        logger.info("接收到线下订单数据：{}", JSON.toJSONString(param));

        orderOfflineService.acceptSellOrderInfo(param);
        return new BaseResult(0, "success", null);
    }

    /**
     * 接收零售系统推送的订单消息(带验签）
     * @return
     */
    @PostMapping("/sell/accept")
    public BaseResult acceptSellOrderInfo(@RequestBody SignVerificationRequest param) {

        logger.info("接收到线下订单数据：{}", JSON.toJSONString(param));

        // 验签
        CheckSignResultDTO dto = SignVerificationUtil.checkSign(param);
        if (dto == null || !dto.getCode()) {
            throw new DefaultException(1, "签名验证失败");
        }

        // 类型转换
        OrderOfflineAddParam dataDTO = JSON.parseObject(param.getParamJson(), OrderOfflineAddParam.class);

        orderOfflineService.acceptSellOrderInfo(dataDTO);
        return new BaseResult(0, "success", null);
    }

}